package org.jeecg.modules.tt_question_collect.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.tt_question_collect.entity.TtCollectFlow;
import org.jeecg.modules.tt_question_collect.entity.TtCollectFlowNode;
import org.jeecg.modules.tt_question_collect.mapper.TtCollectFlowMapper;
import org.jeecg.modules.tt_question_collect.service.ITtCollectFlowNodeService;
import org.jeecg.modules.tt_question_collect.service.ITtCollectFlowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description: 审批流程表
 * @Author: jeecg-boot
 * @Date: 2021-02-02
 * @Version: V1.0
 */
@Service
public class TtCollectFlowServiceImpl extends ServiceImpl<TtCollectFlowMapper, TtCollectFlow> implements ITtCollectFlowService {
    @Autowired
    private TtCollectFlowMapper ttCollectFlowMapper;

    @Autowired
    private ITtCollectFlowService ttCollectFlowService;

    @Autowired
    private ITtCollectFlowNodeService ttCollectFlowNodeService;

    @Override
    public void delall(String flowcode, String id) {
        ttCollectFlowMapper.delbyflowcode(flowcode);
        removeById(id);
    }

    @Override
    @Transactional
    public boolean addInfo(TtCollectFlow ttCollectFlow) {
        Long startTs = System.currentTimeMillis();
        String flowCode = String.valueOf(startTs);
        ttCollectFlow.setFlowcode(flowCode);

        // 查询所选部门上级所有人员
        List<SysUser> userList = ttCollectFlowMapper.queryUserListByDeptId(ttCollectFlow.getDept());
        if (null != userList && userList.size() > 0) {
            TtCollectFlowNode ttCollectFlowNode = new TtCollectFlowNode();
            ttCollectFlowNode.setFlowcode(flowCode);
            ttCollectFlowNode.setNodename("部门直属审核");
            ttCollectFlowNode.setAudittype("指定人员");
            ttCollectFlowNode.setAuditchildtype("全部审核");
            ttCollectFlowNode.setSortid("1");
            ttCollectFlowNode.setShbj("0");
            ttCollectFlowNode.setScbj("0");
            for (SysUser user : userList) {
                ttCollectFlowNode.setAuditid(user.getId());
                ttCollectFlowNode.setBy2(user.getId());
                ttCollectFlowNode.setAuditname(user.getRealname());
                ttCollectFlowNodeService.save(ttCollectFlowNode);
            }
            ttCollectFlowService.save(ttCollectFlow);
            return true;
        }
        return false;
    }

	@Override
	public int queryfacount() {
		// TODO Auto-generated method stub
		return ttCollectFlowMapper.queryfacount();
	}

}
